/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.app2.vista.seguridad;

import com.app2.ejbs.FuncionarioFacade;
import com.app2.entidades.Funcionario;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Diana Vasquez
 */
@RequestScoped
@Named(value = "loginController")
public class LoginController implements Serializable {

    @EJB
    private FuncionarioFacade funcionarioFacade;

    private String user;
    private String clave;
    HttpSession session;

    //setter y getter
    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getClave() {
        return clave;
    }

    public void setClave(String clave) {
        this.clave = clave;
    }

    /**
     *
     * @return 
     *@throws Exception
     */
    public boolean autenticar() throws Exception {
        
        Funcionario encontrado;
        encontrado = funcionarioFacade.autenticar(user, clave);
        FacesContext context = FacesContext.getCurrentInstance();
        
        if (encontrado != null) {
            
            System.out.println(encontrado.getUsuario());
            System.out.println(encontrado.getContrasena());
            context.getExternalContext().getSessionMap().put(user,encontrado.getUsuario());
            context.getExternalContext().getSessionMap().put(clave,encontrado.getContrasena());
            session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            session.setAttribute("sessionUser",user);
            session.setAttribute("sessionClave",clave);
            context.getCurrentInstance().getExternalContext().redirect("faces/Administracion/administracion.xhtml?faces-redirect=true");
            return true;
        } 
        else {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Login Falló!", "usuario/password incorrectos");
            context.addMessage("usuario incorrecto y/o no existe", message);
            return false;
        }
    }

    public String logout() throws IOException {
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getApplicationContextPath();
        System.out.println(contextPath);
        session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

        if (session != null) {
            session.invalidate();
            FacesContext.getCurrentInstance().getExternalContext().redirect(contextPath);
        }
        return null;
    }

}
